package com.xqboss.apps.service.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.user.SysUserWallet;
import com.xqboss.apps.domain.user.SysUserWalletRecord;
import com.xqboss.apps.domain.user.UserAssetWrenchRecord;
import com.xqboss.apps.dto.user.WithdrawRecordListDto;
import com.xqboss.apps.enums.user.UserWalletEnum;
import com.xqboss.apps.enums.user.WalletChangeDirectionEnum;
import com.xqboss.apps.enums.user.WalletChangeTypeEnum;
import com.xqboss.apps.mapper.user.SysUserWalletRecordMapper;
import com.xqboss.apps.mapper.user.SysUserWalletWithdrawMapper;
import com.xqboss.apps.vo.user.SysUserWalletRecordVo;
import com.xqboss.common.exception.ServiceException;
import com.xqboss.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 用户钱包变动记录
 * </p>
 *
 * @author Trent
 * @date 2023/8/22
 */
@Service
public class SysUserWalletRecordService extends ServiceImpl<SysUserWalletRecordMapper, SysUserWalletRecord> {
    @Autowired
    private SysUserWalletWithdrawMapper sysUserWalletWithdrawMapper;

    /**
     * 新增钱包记录
     *
     * @param wallet      钱包
     * @param changeType  改变类型
     * @param direction   改变方向
     * @param amount      变动金额
     * @param afterAmount 变动后金额
     * @param sourceId    源ID：订单支付：订单ID， 用户提现：提现ID，用户充值：订单ID，订单退款:订单ID， 打卡收益：打卡记录ID，队伍收益：队伍收益记录ID
     */
    public void addRecord(SysUserWallet wallet, WalletChangeTypeEnum changeType, WalletChangeDirectionEnum direction, BigDecimal amount, BigDecimal afterAmount, Long sourceId, String remarks) {
        SysUserWalletRecord record = new SysUserWalletRecord();
        record.setUserId(wallet.getUserId());
        record.setWalletId(wallet.getId());
        record.setWalletType(wallet.getType());
        record.setAmount(amount);
        record.setAmountAfter(afterAmount);
        record.setAmountBefore(wallet.getAmount());
        record.setDirection(direction);
        record.setChangeType(changeType);
        record.setSourceId(sourceId);
        try {
            Long userId = SecurityUtils.getUserId();
            record.setOperateUserId(userId);
        } catch (Exception e) {
        }
        record.setRemark(remarks);
        boolean result = save(record);
        if (!result) {
            throw new ServiceException("记录存储失败！");
        }
    }

    /**
     * 获取钱包记录
     *
     * @param record
     * @return
     */
    public List<SysUserWalletRecordVo> getRecordList(WithdrawRecordListDto record) {
        QueryWrapper<SysUserWalletRecord> q = new QueryWrapper<SysUserWalletRecord>()
                .eq(record.getWalletType() != null, "w.wallet_type", record.getWalletType())
                .eq(record.getUserId() != null, "w.user_id", record.getUserId())
                .eq(record.getChangeType() != null, "w.change_type", record.getChangeType())
                .eq(record.getDirection() != null, "w.direction", record.getDirection())
                .eq("w.del_flag",0)
                .ge(record.getCreateTimeStart() != null, "w.create_time", record.getCreateTimeStart())
                .le(record.getCreateTimeEnd() != null, "w.create_time", record.getCreateTimeEnd())
                .orderByDesc("w.id");
        return sysUserWalletWithdrawMapper.getRecordList(q);
    }

    /**
     * 获取扳手钱包记录
     * @param params
     * @return
     */
    public List<SysUserWalletRecordVo> getwrenchRecordList(WithdrawRecordListDto params) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (params.getCreateTimeStart() != null){
            LocalDateTime localDateTimeStart = params.getCreateTimeStart();
            params.setFormattedDateTimeStart(localDateTimeStart.format(formatter));
        }
        if (params.getCreateTimeEnd() != null){
            LocalDateTime localDateTimeEnd = params.getCreateTimeEnd();
            params.setFormattedDateTimeEnd(localDateTimeEnd.format(formatter));
        }

        List<SysUserWalletRecordVo> voList = sysUserWalletWithdrawMapper.getwrenchRecordList(params);
        voList.forEach(e->{
            e.setWalletType(UserWalletEnum.WRENCH);
        });
        return voList;
    }
}
